package com.lingnan.conrtroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.dto.BatchUpdateStatusDTO;
import com.lingnan.dto.JobApplicationQueryDTO;
import com.lingnan.entity.po.CompanyHr;
import com.lingnan.entity.vo.Result;
import com.lingnan.feign.UserFeignClient;
import com.lingnan.service.JobApplicationService;
import com.lingnan.utils.LoginContextHolder;
import com.lingnan.vo.JobApplicationVO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/resume/hr/jobApplication")
@Slf4j
public class HrJobApplicationController {

    @Autowired
    private JobApplicationService jobApplicationService;

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * HR查看所有创建职位的投递记录
     * @param current
     * @param size
     * @param queryDTO
     * @return
     */
    @GetMapping("/list")
    public Result<IPage<JobApplicationVO>> getApplicationList(
            @RequestParam(value = "current",defaultValue = "1") Integer current,
            @RequestParam(value = "size",defaultValue = "10") Integer size,
            JobApplicationQueryDTO queryDTO) {
        if (queryDTO.getStartTime() != null && queryDTO.getStartTime().toString().isEmpty()) {
            queryDTO.setStartTime(null);
        }
        if (queryDTO.getEndTime() != null && queryDTO.getEndTime().toString().isEmpty()) {
            queryDTO.setEndTime(null);
        }
        Long hrId = getUserIdFromToken();
        Page<JobApplicationVO> page = new Page<>(current, size);
        
        IPage<JobApplicationVO> result = jobApplicationService.getApplicationsByHrId(hrId, page, queryDTO);
        return Result.ok(result);
    }

    /**
     * 根据positionId查询职位的投递记录
     * @param positionId
     * @param current
     * @param size
     * @param queryDTO
     * @return
     */
    @GetMapping("/position/{positionId}")
    public Result<IPage<JobApplicationVO>> getApplicationsByPosition(
            @PathVariable("positionId") Long positionId,
            @RequestParam(value = "current",defaultValue = "1") Integer current,
            @RequestParam(value = "size",defaultValue = "10") Integer size,
            JobApplicationQueryDTO queryDTO) {
        
        Page<JobApplicationVO> page = new Page<>(current, size);
        IPage<JobApplicationVO> result = jobApplicationService.getApplicationsByPositionId(positionId, page, queryDTO);
        return Result.ok(result);
    }

    /**
     * hr更新投递记录状态
     * @param applicationId
     * @param status
     * @return
     */
    @PutMapping("/status/{applicationId}")
    public Result updateApplicationStatus(
            @PathVariable("applicationId") Long applicationId,
            @RequestParam("status") Integer status) {
        
        Long hrId = getUserIdFromToken();
        jobApplicationService.updateApplicationStatus(applicationId, status, hrId);
        return Result.ok("状态更新成功");
    }

    /**
     * hr批量更新投递记录状态
     * @param batchUpdateDTO
     */
    @PutMapping("/status/batch")
    public Result batchUpdateStatus(
            @RequestBody BatchUpdateStatusDTO batchUpdateDTO) {
        
        Long hrId = getUserIdFromToken();
        jobApplicationService.batchUpdateApplicationStatus(
            batchUpdateDTO.getApplicationIds(), 
            batchUpdateDTO.getStatus(), 
            hrId
        );
        return Result.ok("批量更新成功");
    }

    /**
     * hr获取所创职位投递记录统计
     * @return
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        Long hrId = getUserIdFromToken();
        Map<String, Object> statistics = jobApplicationService.getApplicationStatistics(hrId);
        return Result.ok(statistics);
    }

    /**
     * hr删除投递记录
     * @param applicationId
     * @return
     */
    @DeleteMapping("/{applicationId}")
    public Result deleteApplication(@PathVariable("applicationId") Long applicationId) {
        boolean result =jobApplicationService.deleteApplication(applicationId);
        return Result.ok(result ? "删除成功" :"删除失败");
    }

    private Long getUserIdFromToken() {
        String loginId = LoginContextHolder.getLoginId();
        CompanyHr hr = userFeignClient.getHrByUserId(Long.valueOf(loginId)).getData();
        return hr.getId();
    }
}